package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.pojo.AgentSta;
import com.tbit.main.service.GeoreGionOperationService;
import com.tbit.main.service.OperationBriefingService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;

/**
 * 运营简报2.0
 *
 * @author LMY
 * @create 2020-11-27 9:27
 */
@RestController
@RequestMapping("/operationBriefing")
public class OperationBriefingController {
    /**
     * 金额格式化
     */
    private DecimalFormat df = new DecimalFormat("0.00");
    @Autowired
    private TokenService tokenService;
    @Autowired
    private OperationBriefingService operationBriefingService;
    @Autowired
    private GeoreGionOperationService georeGionOperationService;

    /**
     * 根据代理商统计订单金额相关数据
     */
    @RequestMapping("/getOrder")
    public JsonResult getOrder(String token, Integer accountId, String startTime, String endTime,
                               @RequestParam(value = "regionId", required = false) Integer regionId) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        AgentSta agentSta = new AgentSta();
        if (regionId == null) {
            agentSta = operationBriefingService.getOrder(accountId, startTime, endTime);
        } else {
            agentSta = georeGionOperationService.getOrder(regionId, startTime, endTime);
        }

        return JsonResult.succ(agentSta);
    }

    /**
     * 根据代理商统计运营相关数据
     */
    @RequestMapping("/getOperation")
    public JsonResult getOperation(String token, Integer accountId, String startTime, String endTime,
                                   @RequestParam(value = "regionId", required = false) Integer regionId) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        AgentSta agentSta = new AgentSta();
        if (regionId == null) {
            agentSta = operationBriefingService.getOperation(accountId, startTime, endTime);
        } else {
            agentSta = georeGionOperationService.getOperation(accountId, regionId, startTime, endTime);
        }

        return JsonResult.succ(agentSta);
    }

    /**
     * 根据代理商统计运维相关数据
     */
    @RequestMapping("/getOPS")
    public JsonResult getOPS(String token, Integer accountId, String startTime, String endTime,
                             @RequestParam(value = "regionId", required = false) Integer regionId) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        AgentSta agentSta = new AgentSta();
        if (regionId == null) {
            agentSta = operationBriefingService.getOPS(accountId, startTime, endTime);
        } else {
            agentSta = georeGionOperationService.getOPS(regionId, startTime, endTime);
        }
        return JsonResult.succ(agentSta);
    }


    /**
     * 根据分区id获取骑行收入
     *
     * @param regionId  分区id
     * @param startTime 开始时间
     * @param endTime   结束时间
     */
    @GetMapping("/getRideIncomeByRegionId")
    public JsonResult getRideIncomeByRegionId(Integer regionId, String startTime, String endTime) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(regionId, "Georegion.regionId.notNull");

        return JsonResult.succ(operationBriefingService.getRideIncomeByRegionId(regionId, startTime, endTime));
    }
}
